package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {

	engine := gin.Default()

	engine.GET("/hello1", func(context *gin.Context) {
		context.Writer.Write([]byte(context.FullPath()))
	})

	engine.GET("/hello2", func(context *gin.Context) {
		context.Writer.WriteString(context.FullPath())
	})

	engine.GET("/hello3", func(context *gin.Context) {
		// 返回的键值对可以自定义名称返回Json
		context.JSON(200, map[string]interface{}{
			"code": 1,
			"msg": "获取成功!",
			"data": context.FullPath(),
		})
		// 按 struct类型后再返回Json
		result := AjaxResult{Code: 1, Msg: "获取成功!", Data: context.FullPath()}
		context.JSON(200, &result)
	})


	// 返回 html
	engine.LoadHTMLGlob("./请求结果的多数据返回/html/*")

	engine.Static("/img", "./img")

	engine.GET("hello4", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{
			"fullPath": context.FullPath(),
			"title": "返回的html",
		})
	})


	if err := engine.Run(":8080"); err != nil {
		log.Fatal(err.Error())
	}

}

type AjaxResult struct {
	Code int
	Msg  string
	Data interface{}
}
